﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BotonJXC.BLL;
using BotonJXC.DAO;
using BotonJXC.Entity;
using BotonJXC.Forms.Events;
using NHibernate;

namespace BotonJXC.Forms.Controls
{
    public partial class RegionControl : BaseInfoControl
    {
        RegionBLL _regionBll = new RegionBLL();
        string _regionName;
        public RegionControl()
        {
            InitializeComponent();
        }

        void LoadData()
        {
            PageModel<Entity.Region> regions = _regionBll.Find(_regionName, pager1.PageStart, pager1.PageSize);
            pager1.TotalCount = regions.TotalCount;
            kdgvRegion.DataSource = new BindingList<Entity.Region>(regions.Results);
        }

        public override IList<object> SelectedItems
        {
            get
            {
                IList<object> selectedItems = new List<object>();
                foreach (DataGridViewRow r in kdgvRegion.SelectedRows)
                {
                    selectedItems.Add(r.DataBoundItem);
                }
                return selectedItems;
            }
        }

        public override void SaveInfo()
        {
            ITransaction trans = HBHelper.BeginTransaction();
            try
            {
                foreach (DataGridViewRow r in kdgvRegion.Rows)
                {
                    Entity.Region region = r.DataBoundItem as Entity.Region;
                    if (region == null)
                        continue;
                    if (region.RegionId == 0)
                    {
                        _regionBll.Save(region);
                    }
                    else if ("1".Equals(r.Tag) && region.RegionId != 0)
                    {
                        _regionBll.Update(region);
                    }
                }
                trans.Commit();
            }
            catch (HibernateException ex)
            {
                trans.Rollback();
            }
            finally
            {
                HBHelper.Close();
            }
            kdgvRegion.Refresh();
        }

        public override void DeleteInfo()
        {
            IList<Entity.Region> regions = (IList<Entity.Region>)kdgvRegion.DataSource;
            ITransaction trans = HBHelper.BeginTransaction();
            try
            {
                foreach (DataGridViewRow r in kdgvRegion.SelectedRows)
                {
                    Entity.Region supplier = r.DataBoundItem as Entity.Region;
                    if (supplier != null)
                    {
                        _regionBll.Delete(supplier.RegionId);
                    }
                    regions.RemoveAt(r.Index);
                }
                trans.Commit();
            }
            catch (HibernateException ex)
            {
                trans.Rollback();
            }
            finally
            {
                HBHelper.Close();
            }
            kdgvRegion.Refresh();
        }

        private void kbQuery_Click(object sender, EventArgs e)
        {
            _regionName = ktbRegionName.Text.Trim();
            LoadData();
        }

        private void pager1_PageChanged(object sender, EventArgs e)
        {
            LoadData();
        }

        private void RegionControl_Load(object sender, EventArgs e)
        {
            if (!DesignMode)
            {
                LoadData();
            }
        }

        private void kdgvRegion_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex > -1)
                kdgvRegion.Rows[e.RowIndex].Tag = "1";
        }
    }
}
